﻿using UnityEngine;
using UnityEditor;
using System.IO;

public class Lch2DDrawer : MaterialPropertyDrawer
{
    public bool editorModel = true;
    public Texture tex;
    public RenderTexture rt;

    public float saturation = 1f;
    public float contrast = 1f;
    public float brightness = 1f;
    
    public Color color = Color.white;

    MaterialProperty self;
    Material editorMat;
    Material mat;
    Material adjustmentMaterial;
    public override void OnGUI(Rect position, MaterialProperty prop, string label, MaterialEditor editor)
    {

        mat = prop.targets[0] as Material;
        if (mat != editorMat)
        {
            mat.SetTexture("_EditingTexture",null)  ;
        }
        editorModel = mat.GetTexture("_EditingTexture")!=null;
        self = prop;
        Rect rect = position;
        position.height = EditorGUIUtility.singleLineHeight * 4;
        if (editorModel)
        {
            Rect r = position;
            r.width = r.height;
            prop.textureValue = rt;
            if(rt)
            EditorGUI.DrawPreviewTexture(r, rt);
            editor.TextureProperty(position, prop, label);

            EditorGUI.BeginChangeCheck();
                rect.y += position.height;
            rect.height = EditorGUIUtility.singleLineHeight;

            // 亮度拖动条
            brightness = EditorGUI.Slider(rect, "亮度", brightness, 0f, 2f);
            rect.y += rect.height;

            // 饱和度拖动条
            saturation = EditorGUI.Slider(rect, "饱和度", saturation, 0f, 2f);
            rect.y += rect.height;

            // 对比度拖动条
            contrast = EditorGUI.Slider(rect, "对比度", contrast, 0f, 2f);
            rect.y += rect.height;

            color = EditorGUI.ColorField(rect, new GUIContent("颜色"), color,true,true,true);
            rect.y += rect.height;
            if (EditorGUI.EndChangeCheck())
            {
                if (null != tex)
                {
                    // 应用饱和度、对比度和亮度调整

                    if (!adjustmentMaterial)
                        adjustmentMaterial = new Material(Shader.Find("Hidden/ColorAdjustment"));
                    adjustmentMaterial.SetFloat("_Saturation", saturation);
                    adjustmentMaterial.SetFloat("_Contrast", contrast);
                    adjustmentMaterial.SetFloat("_Brightness", brightness);
                    adjustmentMaterial.SetColor("_Color", color);
                    //
                    Graphics.Blit(tex, rt, adjustmentMaterial);

                }
            }
            if (GUI.Button(rect, "退出编辑"))
            {
                editorModel = false;

                
                SaveRTToTextureAndSave();
                prop.textureValue = tex;
            }


           
            
        }
        else
        {
            editor.TextureProperty(position, prop, label);
            rect.y += position.height;
            rect.height = EditorGUIUtility.singleLineHeight;
            EditorGUI.BeginDisabledGroup(prop.textureValue == null);
            if (GUI.Button(rect, "编辑饱和度对比度亮度"))
            {
                Reset();
                editorModel = true;
                tex = prop.textureValue;
                editorMat = mat;
                mat.SetTexture("_EditingTexture", prop.textureValue); 
                rt = RenderTexture.GetTemporary(tex.width, tex.height);
                Graphics.Blit(tex, rt);
                prop.textureValue = tex;
            }
            EditorGUI.EndDisabledGroup();
        }
    }
    void Reset()
    {
        saturation = 1f;
        contrast = 1f;
        brightness = 1f;
        color = Color.white;
    }
    private void SaveRTToTextureAndSave()
    {
        if (rt == null || tex == null)
        {
            mat.SetTexture("_EditingTexture", null);
            return;
        }

        // 获取原图片的路径
        string assetPath = AssetDatabase.GetAssetPath(tex);
        string fullPath = Path.Combine(Directory.GetCurrentDirectory(), assetPath);

        // 将 RenderTexture 转换为 Texture2D
        Texture2D texture2D = new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, false);
        RenderTexture.active = rt;
        texture2D.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
        texture2D.Apply();
        RenderTexture.active = null;

        // 保存 Texture2D 为 PNG 文件
        byte[] bytes = ImageConversion.EncodeToPNG(texture2D);
        File.WriteAllBytes(fullPath, bytes);

        // 释放临时资源
        RenderTexture.ReleaseTemporary(rt);
        rt = null;
        Object.DestroyImmediate(texture2D);
        mat.SetTexture("_EditingTexture",null);
        // 刷新资源数据库
        AssetDatabase.Refresh();

        self.textureValue = tex;
        editorMat = null;
    }

    public override float GetPropertyHeight(MaterialProperty prop, string label, MaterialEditor editor)
    {
        if (editorModel)
        {
            return EditorGUIUtility.singleLineHeight * 10;
        }
        else
        {
            return EditorGUIUtility.singleLineHeight * 5;
        }
    }

   
}